package jasperreport;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class UserTest implements Servlet{

	@Override
	public void destroy() {
		
	}

	@Override
	public ServletConfig getServletConfig() {
		return null;
	}

	@Override
	public String getServletInfo() {
		return null;
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		
	}

	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		
		res.setContentType("application/pdf");
		ServletOutputStream out = res.getOutputStream();
		InputStream in = this.getClass().getResourceAsStream("report1.jasper");
		List<User> stus = User.getStudent();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "fugui");
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(stus);
		try {
			JasperRunManager.runReportToPdfStream(in, out, map, dataSource);
		} catch (JRException e) {
			e.printStackTrace();
		} finally {
			
			out.flush();
			out.close();
		}
	}
}
